package com.apps.goods.service.impl;

import com.apps.goods.model.po.KupaGoods;
import com.apps.goods.dao.KupaGoodsMapper;
import com.apps.goods.model.po.KupaGoodsSearchCat;
import com.apps.goods.model.vo.KupaGoodsSearchVo;
import com.apps.goods.service.IKupaGoodsCommentService;
import com.apps.goods.service.IKupaGoodsSearchCatKeyworldService;
import com.apps.goods.service.IKupaGoodsSearchCatService;
import com.apps.goods.service.IKupaGoodsService;
import com.apps.member.service.*;
import com.apps.sannotation.constraints.NumMin;
import com.apps.sannotation.keys.JoinKey;
import com.apps.saspect.ThreadLock;
import com.apps.saspect.ValidMethod;
import com.apps.sexctption.DataException;
import com.apps.sexctption.NullException;
import com.apps.sworld.dao.impl.sDaoImpl;
import com.apps.thirdparty.solr.service.IKupaSolrService;
import com.apps.thirdparty.solr.utils.IKupaSolrUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 大鹏
 * @since 2020-10-02
 */
@Slf4j
@Service
public class KupaGoodsServiceImpl extends sDaoImpl<KupaGoodsMapper, KupaGoods> implements IKupaGoodsService
{

    @Autowired
    private IKupaMemberCollectGoodsService collectGoodsService;

    @Autowired
    private IKupaGoodsCommentService commentGoodsService;

    @Autowired
    private IKupaMemberLikeGoodsService likeGoodsService;

    @Autowired
    private IKupaGoodsSearchCatService goodsSearchCatService;

    @Autowired
    private IKupaGoodsSearchCatKeyworldService goodsSearchCatKeyworldService;

    @Autowired
    private IKupaSolrService solrService;

    @ThreadLock
    @Override
    public int addCollectNumber(@JoinKey Integer goods_id, int number)
    {
        KupaGoods goods=getOneById(goods_id);
        if(null == goods)
            throw new NullException("商品不存在");
        if((goods.getGoods_collect_num().intValue()+number) < 0) {
            int n_num=collectGoodsService.countByGoodsId(goods_id);
            if(n_num+number < 0)
               throw new DataException("商品被收藏数据异常");
            else
                goods.setGoods_collect_num(n_num);
        }
        goods.setGoods_collect_num(goods.getGoods_collect_num().intValue()+number);
        updateById(goods);
        return goods.getGoods_collect_num();
    }

    @ThreadLock
    @Override
    public int addLikeNumber(@JoinKey Integer goods_id,@NumMin(min = 0,errMsg = "必须大于0") int number) {
        KupaGoods goods=getOneById(goods_id);
        if(null == goods)
            throw new NullException("商品不存在");
        if((goods.getGoods_like_num().intValue()+number) < 0) {
            int n_num=likeGoodsService.countByGoodsId(goods_id);
            if(n_num+number < 0)
               throw new DataException("商品被点赞数据异常");
            else
                goods.setGoods_like_num(n_num);
        }
        goods.setGoods_like_num(goods.getGoods_like_num().intValue()+number);
        updateById(goods);
        return goods.getGoods_like_num();
    }
    @ThreadLock
    @Override
    public int addCommentNumber(@JoinKey Integer goods_id,@NumMin(min = 0,errMsg = "必须大于0") int number) {
        KupaGoods goods=getOneById(goods_id);
        if(null == goods)
            throw new NullException("商品不存在");
        if((goods.getGoods_comment_num().intValue()+number) < 0) {
            int n_num=commentGoodsService.countByGoodsId(goods_id);
            if(n_num+number < 0)
               throw new DataException("商品被评论数据异常");
            else
                goods.setGoods_comment_num(n_num);
        }
        goods.setGoods_comment_num(goods.getGoods_comment_num().intValue()+number);
        updateById(goods);
        return goods.getGoods_comment_num();
    }

    @Override
    public List<KupaGoods> resembleRecommend(KupaGoods goods,int size) {
        return new ArrayList<>();
    }

    @Override
    public List<KupaGoodsSearchVo> queryGoodsSearchParam(Integer goods_cat_id)
    {
        List<KupaGoodsSearchVo> redsult=new ArrayList<>();
        List<KupaGoodsSearchCat> goodsSearchCats=goodsSearchCatService.queryByCatId(goods_cat_id);
        for(KupaGoodsSearchCat searchCat:goodsSearchCats)
        {
            redsult.add(new KupaGoodsSearchVo(searchCat.getName(),goodsSearchCatKeyworldService.queryByCatId(searchCat.getId())));
        }
        return redsult;
    }

    @SneakyThrows
    @Override
    public IPage<KupaGoods> searchGoods(String keyworld, List<Integer> condition, Integer goods_cat_id, Integer pageNo, Integer pageSize)
    {
        if(StringUtils.isEmpty(keyworld))
            keyworld="*";
        IPage<KupaGoods> result=new Page<KupaGoods>();
        result.setRecords(new ArrayList<>());
        result.setCurrent(pageNo);
        List<KupaGoodsSearchVo> goodsSearchCats=queryGoodsSearchParam(goods_cat_id);
        if(goodsSearchCats.size() != condition.size())
            throw new DataException("查询附加条件数据异常");
        for(int i=0;i<condition.size();i++)
        {
            if(condition.get(i).intValue() > 0)
               keyworld += " && goods_name:"+ goodsSearchCats.get(i).getRecord().get(condition.get(i)).getName();
        }
        //添加分类查询
        keyworld+=" && goods_cat_id:" + goods_cat_id;
        //调用solr查询
        SolrDocumentList documentList=solrService.search("goods_name:"+keyworld,pageNo,pageSize);
        if(!documentList.isEmpty())
        {
            for(SolrDocument document:documentList)
            {
               result.getRecords().add(new KupaGoods(document));
            }
        }
        return result;
    }

}
